package com.chinasoft.mall.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.chinasoft.mall.R;
import com.chinasoft.mall.framework.BeanCache;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.LoginCheckBean;

/**
 * 员工商城登陆页面
 * 
 * @author Damen
 * 
 */
public class StaffLoginActivity extends BaseActivity {

	private BaseBean loginBean;

	public static final String USER_NO = "userno";

	public static final String USER_NAME = "username";

	private EditText userNo;

	private EditText userName;

	private ImageView staffLogin;

	private static final String LOGIN_SUCESS = "000000";
	
	private TextWatcher watcher = new TextWatcher(){ 
		  
	    @Override
	    public void afterTextChanged(Editable s) { 
	        // TODO Auto-generated method stub 
	    } 
	  
	    @Override
	    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
	        // TODO Auto-generated method stub 
	    } 
	  
	    @Override
	    public void onTextChanged(CharSequence s, int start, int before, int count) { 
	       loginBean = null;
	    } 
	};
	      


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepage_login);
		userNo = (EditText) findViewById(R.id.user_no);
		userName = (EditText) findViewById(R.id.user_name);
	
		
		staffLogin = (ImageView) findViewById(R.id.staff_login_btn);

		loginBean = BeanCache.getInstance().getResponsdata().get(Config.LOGIN_CHECK);

		staffLogin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				initRequest();
				hideSoftInput();
			}
		});

		SharedPreferences mSharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
		boolean tag = mSharedPreferences.getBoolean("value", false);
		if (tag) {

			if (loginBean != null
					&& loginBean.getResData() != null
					&& (LoginCheckBean) loginBean.getResData().getData() != null
					&& ((LoginCheckBean) loginBean.getResData().getData())
							.getReturnCode().equals(LOGIN_SUCESS)) {
			
			userName.setText("******");
			userName.setEnabled(false);
//			userName.setTransformationMethod(PasswordTransformationMethod
//					.getInstance());
			userNo.setText("******");
			userNo.setEnabled(false);
//			userNo.setTransformationMethod(PasswordTransformationMethod
//					.getInstance());
			}else{
				SharedPreferences share = getSharedPreferences("login", Context.MODE_PRIVATE);    
				Editor editor = share.edit();           
				editor.putBoolean("value", false);
				editor.commit();
			}
			
		}
		
	}


	public void hideSoftInput() {
		InputMethodManager imm = (InputMethodManager) getSystemService(StaffLoginActivity.this.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(userNo.getWindowToken(), 0);
	}

	private void initRequest() {

		SharedPreferences mSharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
		boolean tag = mSharedPreferences.getBoolean("value", false);
		if (loginBean != null
				&& loginBean.getResData() != null
				&& (LoginCheckBean) loginBean.getResData().getData() != null
				&& ((LoginCheckBean) loginBean.getResData().getData())
						.getReturnCode().equals(LOGIN_SUCESS)&&tag) {
			loginBean.reqData.put(USER_NO, userNo.getText().toString().trim());
			
			loginBean.reqData.put(USER_NAME, userName.getText().toString().trim());
			loginBean.setUrl_type((byte) 1);
			loginBean.setReq_type((byte) 5);
			success(loginBean);
		} else {
			loginBean = new BaseBean(Config.LOGIN_CHECK);
			loginBean.reqData.put(USER_NO, userNo.getText().toString().trim());
			loginBean.reqData.put(USER_NAME, userName.getText().toString().trim());
			loginBean.setUrl_type((byte) 1);
			loginBean.setReq_type((byte) 5);
			control.sendMessage(loginBean);
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if (obj.equals(loginBean)) {
			if (obj.getResData() != null && obj.getResData().getData() != null) {

				LoginCheckBean checkBean = (LoginCheckBean) obj.getResData()
						.getData();
				String code = checkBean.getReturnCode();
				if (LOGIN_SUCESS.equals(code)) {
					((MainActivity) StaffLoginActivity.this.getParent())
							.setCurrentActivity(StaffGoodsClassify.class, this
									.getString(R.string.staff_goods_classify));
					SharedPreferences mSharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);    
					Editor editor = mSharedPreferences.edit();           
					editor.putBoolean("value", true);
					editor.commit();
				} else {
					Toast.makeText(this, checkBean.getDesc(), 500).show();
				}

			}
		}
	}

}
